#	可选命令：
#			make clean
#			make delete
#			make clang
#			make release
#	这些makefile远远不够健壮，可移植性非常低。
#
#        Copyright (c) 2021 LiuBingyi
#        SimpleMark is licensed under Mulan PubL v2.
#        You can use this software according to the terms and conditions of the Mulan PubL v2.
#        You may obtain a copy of Mulan PubL v2 at:
#                 http://license.coscl.org.cn/MulanPubL-2.0
#  	     THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#        EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#        MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#        See the Mulan PubL v2 for more details.

ifeq ($(OS),Windows_NT)
#	DSC(DirectorySeparatorChar,目录分隔符)，如果遇到兼容性问题可以启用
#ifeq ($(findstring :,$(SHELL)),:)
#endif
	ISNTOS := true
endif
ifeq ($(ISNTOS),true)
	DSC ?= $(if OS,\,/)
	dySuffix := .dll
	exeSuffix := .exe
else
	dySuffix := .so
	fpic := -fPIC
	DSC ?= /
endif

#这个变量用于作为make release时传递进make的参数
eval_release += userFlag+=-O3 userFlag+=-DNDEBUG
ifeq ($(cc), gcc)
#解决clang中部分命令和gcc不一致的问题
	fpic := -fPIC
	eval_release += userFlag+=-s
endif
ifeq ($(cc), clang)
#	userFlag+=--target=x86_64-w64-windows-gnu
#	因为clang默认使用msvc，但是我还没办法传递正确的参数使之编译成功
	userFlag+=--target=x86_64-w64-windows-gnu
endif

ifndef $(findstring cmd,$(SHELL))
	DSC := /
endif

cc ?= gcc
userFlag += -Wall
shared := .$(DSC)shared$(DSC)
include := .$(DSC)include$(DSC)
bin := .$(DSC)bin$(DSC)
src := .$(DSC)src$(DSC)
independent_inc := $(include)independent$(DSC)
bricks_inc := $(include)bricks$(DSC)

dylib := basic bricks
objects := $(shared)main.o $(bin)basic$(dySuffix) $(bin)bricks$(dySuffix)

VPATH := $(shared) $(include) $(src) $(bin)

cls := $(shared)*.o $(bin)SimpleMark$(exeSuffix) $(bin)basic$(dySuffix) $(bin)bricks$(dySuffix) 

SimpleMark : $(objects)
	$(cc) $^ -o $(bin)$@ -I $(include) -L $(bin) $(userFlag)
$(shared)main.o : $(src)main.c $(include)main.h
	$(cc) -c $(src)main.c -o $@ -I$(include) -I$(independent_inc) -I$(bricks_inc) $(userFlag)
include $(bricks_inc)bricks.mk
include $(independent_inc)independent.mk




.PHONY : clean echo delete clang release
echo:
	-@echo $(findstring :,$(SHELL))
	-@echo $(SHELL)
	-@echo $(cc)
	-@echo $(userFlag)


clean :
ifeq ($(ISNTOS),true)
	$(warning Your OS may not be Unix-like! \
	if you get an Error,Please try to use "make delete"!)
# 	-@ $(MAKE) --no-print-directory delete
# else
endif
	-rm -f $(cls)
# endif

delete :
#clean for Windows
	-del $(subst /,\\,$(cls))

clang :
	-@$(MAKE) -e cc=clang $(eval_clang) 

release:
	-@$(MAKE) -bB --no-print-directory $(eval_release)
